{* Smarty *}

{function name=pagination form_id='' page=1 total=1}
{if $total > 1}
{$display_num=5}
{$half_num=$display_num/2}
{$half_num=$half_num|intval}
<table class="pages" width="240" cellspacing="0" cellpadding="0">
    <tr>
        <td width="100">
        {if $page != 1}
            <a id="pre_page" href="#">上一页</a>
        {/if}

        {if $total <= $display_num}
            {$begin=1}
            {$end=$total}
        {elseif $page + $half_num >= $total}
            {$begin=$total-$display_num+1}
            {$end=$total}
        {elseif $page<=$half_num}
            {$begin=1}
            {$end=$display_num}
        {else}
            {$begin=$page-$half_num}
            {$end=$page+$half_num}
        {/if}

        {for $i=$begin to $end}
            {if $page == $i}
            <a class="on">{$i}</a>
            {else}
            <a class="go_page" data-page="{$i}" href="#">{$i}</a>
            {/if}
        {/for}

        {if $page != $total}
            <a id="next_page" href="#">下一页</a>
        {/if}
        </td>
    </tr>
</table>
<script type="text/javascript">
(function($)
{
    var form = $('#{$form_id}');
    var curr_page = {$page};

    function go_page(page)
    {
        if($('input[name="page"]').size() == 0)
        {
            $('<input type="hidden" name="page" />').appendTo(form);
        }
        $('input[name="page"]').val(page);

        form.submit();
    }

    $(".go_page").click(function()
    {
        var page = $(this).data('page');
        go_page(page);
    }
    );

    $('#pre_page').click(function()
    {
        var page = curr_page - 1;
        go_page(page);
    }
    );

    $('#next_page').click(function()
    {
        var page = curr_page + 1;
        go_page(page);
    }
    );
}
)(jQuery);
</script>
{/if}
{/function}
